//
//  CustomMovieInfoView.m
//  ShowTimeApp
//
//  Created by Ruby on 10/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CustomMovieInfoView.h"
#import "Movie.h"
#import "CustomShowtimeView.h"

@implementation CustomMovieInfoView
@synthesize titleViLabel,countryLabel,directorLabel,actorLabel,genreLabel,languageLabel,versionLabel,lengthLabel,movieStudioLabel,descriptionLabel;
@synthesize viewTrailerCommand,showtimeCommand;
@synthesize posterImageView,movie;

- (id)initWithFrame:(CGRect)frame andText:(NSString*)string
{
    self = [super initWithFrame:frame];
    if (self) {
        //Load view from xib
//        NSArray* screens = [[NSBundle mainBundle]loadNibNamed:@"CustomMovieInfoView" owner:self options:nil];
//        [self addSubview:[screens objectAtIndex:0]];

        NSString *filename = [NSString stringWithFormat:@"image%d.png", 3];
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
        iv.frame = CGRectMake(0.0f, 0.0f, 128.0f, 128.0f);
        [self addSubview:iv];
    }
    return self;
}

-(id)initWithMovieInfo:(Movie *)_movie andFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){        
        self.movie = _movie;
        if(self.movie){
            NSString *filename = @"movieInfoBG320x368.png";
            UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
            iv.frame = frame;
            
            titleViLabel = [[UILabel alloc]initWithFrame:CGRectMake(15.0f, 10.0f, 289.0f, 19.0f)];
            [titleViLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            titleViLabel.text = self.movie.TitleVI;
            [titleViLabel setBackgroundColor:[UIColor clearColor]];
            [titleViLabel setTextColor:[UIColor yellowColor]];
            [iv addSubview:titleViLabel];
            
            posterImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 37.0f, 160.0f, 160.0f)];
            //posterImageView.image= [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://megastar.vn/images/locations/353x257/19_5.jpg"]]];
            posterImageView.image= [UIImage imageNamed:@"stock-illustration-2649055-movie-stars.jpg"];
            [iv addSubview:posterImageView];
            
            genreLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 37.0f, 125.0f, 21.0f)];
            [genreLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            genreLabel.text = self.movie.Genre;
            [genreLabel setBackgroundColor:[UIColor clearColor]];
            [genreLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:genreLabel];
            
            countryLabel = [[UILabel alloc]initWithFrame:CGRectMake(131.0f, 249.0f, 125.0f, 21.0f)];
            [countryLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            countryLabel.text = [NSString stringWithFormat:@"Country: %@",self.movie.Country];
            [countryLabel setBackgroundColor:[UIColor clearColor]];
            [countryLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:countryLabel];
            
            directorLabel = [[UILabel alloc]initWithFrame:CGRectMake(131.0f, 220.0f, 125.0f, 21.0f)];
            [directorLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            directorLabel.text = [NSString stringWithFormat:@"Director: %@",self.movie.Director];
            [directorLabel setBackgroundColor:[UIColor clearColor]];
            [directorLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:directorLabel];
            
            actorLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 37.0f, 125.0f, 59.0f)];
            [actorLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            actorLabel.text = self.movie.Actors;
            [actorLabel setBackgroundColor:[UIColor clearColor]];
            [actorLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:actorLabel];
            
            languageLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 113.0f, 125.0f, 21.0f)];
            [languageLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            languageLabel.text = self.movie.Language;
            [languageLabel setBackgroundColor:[UIColor clearColor]];
            [languageLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:languageLabel];
            
            versionLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 147.0f, 125.0f, 21.0f)];
            [versionLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            versionLabel.text = [NSString stringWithFormat:@"Version: %@",self.movie.Version];
            [versionLabel setBackgroundColor:[UIColor clearColor]];
            [versionLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:versionLabel];
            
            lengthLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 174.0f, 125.0f, 21.0f)];
            [lengthLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            lengthLabel.text =  [NSString stringWithFormat:@"Length: %@", [self.movie.Length stringValue]];
            [lengthLabel setBackgroundColor:[UIColor clearColor]];
            [lengthLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:lengthLabel];
            
            movieStudioLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 86.0f, 125.0f, 21.0f)];
            [movieStudioLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            movieStudioLabel.text = self.movie.MovieStudio;
            [movieStudioLabel setBackgroundColor:[UIColor clearColor]];
            [movieStudioLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:movieStudioLabel];
            
            descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(13.0f, 296.0f, 298.0f, 74.0f)];
            [descriptionLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            descriptionLabel.text = self.movie.Description;
            [descriptionLabel setBackgroundColor:[UIColor clearColor]];
            [descriptionLabel setTextColor:[UIColor whiteColor]];
            descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
            descriptionLabel.numberOfLines=0;
            [descriptionLabel sizeToFit];
            [iv addSubview:descriptionLabel];
            
//            viewTrailerCommand = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//            [viewTrailerCommand setFrame:CGRectMake(16.0f, 220.0f, 100.0f, 28.0f)];
//            [viewTrailerCommand setTitle:@"Xem Trailer" forState:UIControlStateNormal];
//            [viewTrailerCommand addTarget:self action:@selector(showTrailer:) forControlEvents:UIControlEventTouchUpInside];
//            [iv addSubview:viewTrailerCommand];
//            
//            showtimeCommand = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//            [showtimeCommand setFrame:CGRectMake(16.0f, 259.0f, 100.0f, 28.0f)];
//            [showtimeCommand setTitle:@"Showtime" forState:UIControlStateNormal];
//            [showtimeCommand addTarget:self action:@selector(showCurrentShowtime:) forControlEvents:UIControlEventTouchUpInside];
//            [iv addSubview:showtimeCommand];   
            
            [self addSubview:iv];
        }
    }
    return self;
}

-(id)initWithMovieInfo:(Movie *)_movie andFrame:(CGRect)frame andController:(UIViewController *)controller{
    self=[super initWithFrame:frame];
    if(self){        
        self.movie = _movie;
        if(self.movie){
            NSString *filename = @"movieInfoBG320x368.png";
            UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
            iv.frame = frame;
            
            titleViLabel = [[UILabel alloc]initWithFrame:CGRectMake(15.0f, 10.0f, 289.0f, 19.0f)];
            [titleViLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            titleViLabel.text = self.movie.TitleVI;
            [titleViLabel setBackgroundColor:[UIColor clearColor]];
            [titleViLabel setTextColor:[UIColor yellowColor]];
            [iv addSubview:titleViLabel];
            
            posterImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 37.0f, 160.0f, 160.0f)];
            posterImageView.image= [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://megastar.vn/images/locations/353x257/19_5.jpg"]]];
            [iv addSubview:posterImageView];
            
            genreLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 37.0f, 125.0f, 21.0f)];
            [genreLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            genreLabel.text = self.movie.Genre;
            [genreLabel setBackgroundColor:[UIColor clearColor]];
            [genreLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:genreLabel];
            
            countryLabel = [[UILabel alloc]initWithFrame:CGRectMake(131.0f, 249.0f, 125.0f, 21.0f)];
            [countryLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            countryLabel.text = self.movie.Country;
            [countryLabel setBackgroundColor:[UIColor clearColor]];
            [countryLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:countryLabel];
            
            directorLabel = [[UILabel alloc]initWithFrame:CGRectMake(131.0f, 220.0f, 125.0f, 21.0f)];
            [directorLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            directorLabel.text = self.movie.Director;
            [directorLabel setBackgroundColor:[UIColor clearColor]];
            [directorLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:directorLabel];
            
            actorLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 37.0f, 125.0f, 59.0f)];
            [actorLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            actorLabel.text = self.movie.Actors;
            [actorLabel setBackgroundColor:[UIColor clearColor]];
            [actorLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:actorLabel];
            
            languageLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 113.0f, 125.0f, 21.0f)];
            [languageLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            languageLabel.text = self.movie.Language;
            [languageLabel setBackgroundColor:[UIColor clearColor]];
            [languageLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:languageLabel];
            
            versionLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 147.0f, 125.0f, 21.0f)];
            [versionLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            versionLabel.text = self.movie.Version;
            [versionLabel setBackgroundColor:[UIColor clearColor]];
            [versionLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:versionLabel];
            
            lengthLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 174.0f, 125.0f, 21.0f)];
            [lengthLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            lengthLabel.text = [self.movie.Length stringValue];
            [lengthLabel setBackgroundColor:[UIColor clearColor]];
            [lengthLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:lengthLabel];
            
            movieStudioLabel = [[UILabel alloc]initWithFrame:CGRectMake(178.0f, 86.0f, 125.0f, 21.0f)];
            [movieStudioLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            movieStudioLabel.text = self.movie.MovieStudio;
            [movieStudioLabel setBackgroundColor:[UIColor clearColor]];
            [movieStudioLabel setTextColor:[UIColor whiteColor]];
            [iv addSubview:movieStudioLabel];
            
            descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(13.0f, 296.0f, 298.0f, 74.0f)];
            [descriptionLabel setFont:[UIFont fontWithName:@"Helvetica" size:13]];
            descriptionLabel.text = self.movie.Description;
            [descriptionLabel setBackgroundColor:[UIColor clearColor]];
            [descriptionLabel setTextColor:[UIColor whiteColor]];
            descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
            descriptionLabel.numberOfLines=0;
            [descriptionLabel sizeToFit];
            [iv addSubview:descriptionLabel];
            
            viewTrailerCommand = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [viewTrailerCommand setFrame:CGRectMake(16.0f, 220.0f, 100.0f, 28.0f)];
            [viewTrailerCommand setTitle:@"Xem Trailer" forState:UIControlStateNormal];
            [viewTrailerCommand addTarget:self action:@selector(showTrailer:) forControlEvents:UIControlEventTouchUpInside];
            [iv addSubview:viewTrailerCommand];
            
            showtimeCommand = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [showtimeCommand setFrame:CGRectMake(16.0f, 259.0f, 100.0f, 28.0f)];
            [showtimeCommand setTitle:@"Showtime" forState:UIControlStateNormal];
            [showtimeCommand addTarget:self action:@selector(showCurrentShowtime:) forControlEvents:UIControlEventTouchUpInside];
            [iv addSubview:showtimeCommand];   
            
            [self addSubview:iv];
            controller.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Showtime" style:UIBarButtonItemStylePlain target:self action:@selector(showTrailer:)] autorelease];
        }
    }
    return self;
}

-(void)showTrailer:(id)sender{
    CGContextRef context = UIGraphicsGetCurrentContext();
	[UIView beginAnimations:nil context:context];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationDuration:1.0];
    [self setAlpha:0.0f];
    CustomShowtimeView *showtimeView=[[CustomShowtimeView alloc]initWithFrame:self.frame];
    [showtimeView setAlpha:1.0f];
    self.transform = CGAffineTransformMakeScale(0.25f, 0.25f);
	showtimeView.transform = CGAffineTransformIdentity;
	[self exchangeSubviewAtIndex:1 withSubviewAtIndex:2];
    
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
	[UIView commitAnimations];
}

-(id)showCurrentShowtime:(id)sender{
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
